package net.pimpas.web.jsf.components.ajax.poolsize;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

import net.pimpas.web.jsf.components.UIComponentPimpasBaseTag;
import net.pimpas.web.jsf.util.ComponentUtils;

public class UIAjaxPoolSizeTag extends UIComponentPimpasBaseTag {
	
	private String value;
	
	@Override
	protected void setProperties(UIComponent component) {
		super.setProperties(component);
		
		UIAjaxPoolSize pool = (UIAjaxPoolSize) component;
		// check valuebinding
		Integer v = null;
		if(this.getValue() != null && !"".equals(this.getValue())) {
			if(ComponentUtils.isOnlyValueBinding(this.getValue())) {
				v = (Integer) FacesContext.getCurrentInstance().getApplication().
				createValueBinding(this.getValue()).getValue(FacesContext.getCurrentInstance());
			} else {
				try {
					v = Integer.parseInt(this.getValue());
				} catch(NumberFormatException e) {
					v = UIAjaxPoolSize.DEFAULT_POOL_SIZE;
				}
			}
		} else
			v = UIAjaxPoolSize.DEFAULT_POOL_SIZE;
		
		pool.setValue(v);
	}

	@Override
	public String getComponentType() {
		return UIAjaxPoolSize.COMPONENT_TYPE;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
}